/***
 * * PROJECT
 * *    Logical Neighborhood Routing
 * * VERSION
 * *    $Revision: 1.3 $
 * * DATE
 * *    $Date: 2007/06/15 06:11:19 $
 * * LOG
 * *    $Log: ScopedChannel.java,v $
 * *    Revision 1.3  2007/06/15 06:11:19  animesh
 * *    no message
 * *
 * *    Revision 1.2  2007/06/09 07:15:49  animesh
 * *    update after fixing all but sets and serialization.
 * *
 * *    Revision 1.5  2007/01/15 09:11:14  animesh
 * *    added getInterestLabel()
 * *
 * *    Revision 1.4  2006/11/03 23:20:57  lmottola
 * *    maxHops renamed.
 * *
 * *    Revision 1.3  2006/10/26 22:15:56  animesh
 * *    getTaskID and getDataID fixed from their default "return 0".
 * *
 * *    Revision 1.2  2006/10/25 22:32:18  animesh
 * *    added blank constructor
 * *
 * *    Revision 1.1  2006/10/25 03:30:55  animesh
 * *    Moved this to the atag.common package. In some time, ATaGChannelDeclaration will be deleted.
 * *
 * *    Revision 1.8  2006/10/24 09:56:28  animesh
 * *    Changed getOutputChannelDeclaration to public to let Dispatcher access it
 * *
 * *    Revision 1.7  2006/10/24 00:21:09  animesh
 * *    no message
 * *
 * *    Revision 1.6  2006/10/23 22:00:11  animesh
 * *    no message
 * *
 * *    Revision 1.5  2006/10/21 00:17:54  animesh
 * *    no message
 * *
 * *    Revision 1.4  2006/10/21 00:07:42  animesh
 * *    no message
 * *
 * *    Revision 1.3  2006/10/20 23:48:23  lmottola
 * *    Added list of attributes.
 * *
 * *    Revision 1.2  2006/10/19 21:21:52  lmottola
 * *    Added CVS preamble.
 * *
 * *
 * *   Logical Neighborhoods Routing - Java Version
 * *
 * *
 * *   This program is free software; you can redistribute it and/or
 * *   modify it under the terms of the GNU General Public License
 * *   as published by the Free Software Foundation; either version 2
 * *   of the License, or (at your option) any later version.
 * *
 * *   This program is distributed in the hope that it will be useful,
 * *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 * *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * *   GNU General Public License for more details.
 * *
 * *   You should have received a copy of the GNU General Public License
 * *   along with this program; if not, you may find a copy at the FSF web
 * *   site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
 * *   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * *   Boston, MA  02111-1307, USA
 ***/

package atag.common;

//import java.util.Collection;
//import java.util.HashSet;



public class ScopedChannel {
    
    public static final int CHANNEL_INIT_PUSH = 0x01;
    public static final int CHANNEL_INIT_PULL = 0x02;
    
    public static final int CHANNEL_TYPE_INPUT = 0x01;
    public static final int CHANNEL_TYPE_OUTPUT = 0x02;
    public static final int CHANNEL_TYPE_REQUEST_RESPONSE = 0x04;

    /**
     * Channel interests. "local" is specified separately
     */
    public static final int CHANNEL_INTEREST_NONE = 0x0;
    public static final int CHANNEL_INTEREST_LOGICAL_HOP = 0x01;
    public static final int CHANNEL_INTEREST_NEIGHBOR_HOP = 0x02;
    public static final int CHANNEL_INTEREST_NEIGHBOR_DISTANCE = 0x04;
    public static final int CHANNEL_INTEREST_ALL_NODES = 0x08;
    public static final int CHANNEL_INTEREST_DOMAIN = 0x10;
    
    // TODO Animesh -- we need this at a later stage
    // Currently the ATaGManager has hardcoded predicates
    //private HashSet<LNPredicate> predicates;
    //private HashSet predicates;
    
    //private LNPredicate[] predicates;
    
    private byte maxHops;
    
    private String taskName;
    private String dataName;
    /**
     * interest - specified as a string. Should later be an int chosen from a 
     * set of static finals
     */
    private String interest;
    private int direction;
    
    /**
     * whether label is "local" or not
     */
    private boolean isLocal;
    
    /**
     * unique ID. To be set during sanitization of code [checking for dupes etc.]
     */
    private int ID;
    
    /**
     * task ID. To be set during sanitization of code [checking for dupes etc.]
     */
    private int taskID;
    
    /**
     * data ID. To be set during sanitization of code [checking for dupes etc.]
     */
    private int dataID;
    
    /** blank constructor
     */
    public ScopedChannel(){
        
    }
    
    public ScopedChannel(int taskid, int dataid, String ioType, String ppType,
            String cInterest, int interestParam) {
        //this.predicates = new HashSet();
    }
    
    /**
     * sacred to Luca
     */
//    public Collection getPredicates() {
//        return predicates;
//    }
//    
    /**
     * to be Implemented by Animesh as a variant of addInterest
     */
//    public void addPredicate(LNPredicate p) {
//        predicates.add(p);
//    }
    
    
    public int taskID() {
        // TODO Auto-generated method stub
        return this.taskID;
    }
    
    public int dataID() {
        // TODO Auto-generated method stub
        return this.dataID;
    }
    
   
    public boolean isOutput() {
        return (this.direction == ScopedChannel.CHANNEL_TYPE_OUTPUT);
    }

    public boolean isInput() {
        return (this.direction == ScopedChannel.CHANNEL_TYPE_INPUT);
    }

    public boolean isRequestResponse() {
		return (this.direction == ScopedChannel.CHANNEL_TYPE_REQUEST_RESPONSE);
	}

    /**
     * sacred to Luca
     */
    public byte getMaxNetworkHops() {
        return this.maxHops;
    }
    
    /**
     * set the MaxHops. Something has to set this at init.
     */
    public void setMaxNetworkHops(byte maxHops) {
        this.maxHops = maxHops;
    }
    
    
    /**
     * @returns if the channel uses hops at all
     */
    public boolean involvesNetworkHops(){
        // TODO CHANGE THIS!!
        return false;
    }
    
    //Animesh -- functions from the Channel Description
    public void setTaskName(String newTaskName){
        this.taskName = new String(newTaskName);
    }
    
    public String getTaskName(){
        return this.taskName;
    }
    
    public void setDataName(String newDataName){
        this.dataName = new String(newDataName);
    }
    
    public String getDataName(){
        return this.dataName;
    }
    public void setDirection(int newDirection){
        this.direction = newDirection;
    }
    
    public int getDirection(){
        return this.direction;
    }
    
    public int getID() {
        return this.ID;
    }
    
    public void setID(int newID) {
        this.ID = newID;
    }
    
    public int getTaskID() {
        return this.taskID;
    }
    
    public void setTaskID(int newTaskID) {
        this.taskID = newTaskID;
    }
    public int getDataID() {
        return this.dataID;
    }
    
    public void setDataID(int newDataID) {
        this.dataID = newDataID;
    }
    
    public String getTypeInString(){
        String retString = "";
        if (this.direction == ScopedChannel.CHANNEL_TYPE_OUTPUT)
            return new String("OUTPUT");
        else
            return new String("INPUT");
        
    }
    
    
    public void setLocalFlag(boolean newLocalFlag){
        this.isLocal = newLocalFlag;
    }
    
    public boolean isLocal() {
        //check if the interest string has a local attribute
        //change this to a boolean local operater later
        return this.isLocal;
    }
    
    public void setInterest(String newInterest){
        this.interest = new String(newInterest);
    }
    
    public String getInterest(){
        // parse the interest string and pick out the part before the colon
        //return this.interest.split(":")[0];
        return StringHelper.splitString(this.interest, ':')[0];
    }
    
    public String getInterestParam(){
        // parse the interest string and pick out the part after the colon
        String retString = null;
        try{
//            retString = this.interest.split(":")[1];
            retString = StringHelper.splitString(this.interest, ':')[1];
        }catch (ArrayIndexOutOfBoundsException ex){
            System.out.println("This "+this.getTypeInString()+" channel ("
                    + this.taskName + " " + this.dataName +" has interest string" +
                    this.interest + " which does not have a colon!");
            ex.printStackTrace();
        }
        
        return retString;
    }
    
    public String getInterestLabel(){
        // parse the interest string and pick out the part after the 2nd colon
        String retString = null;
        try{
            //retString = this.interest.split(":")[2];
            retString = StringHelper.splitString(this.interest, ':')[2];
        }catch (ArrayIndexOutOfBoundsException ex){
            System.out.println("This "+this.getTypeInString()+" channel ("
                    + this.taskName + " " + this.dataName +" has interest string" +
                    this.interest + " which does not have a second colon!");
            ex.printStackTrace();
        }
        
        return retString;
    }
    
    
}
